package com.yc.Testjvm.invoke;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class TestSon {
	
	class GrandFather{
	    void thinking() {
	        System.out.println("i am grandfather");
	    }
	}

	class Father extends GrandFather{
	    void thinking() {
	        System.out.println("i am father");
	    }
	}

	class Son extends Father{
	    void thinking() {
	        //请在这里填入适当的代码（不能修改其他地方的代码）
	        //实现调用祖父类的thinking（）方法，打印"i am grandfather"
//	    	System.out.println("i am son");
//	    	GrandFather gf = new GrandFather();
//	    	gf.thinking();
	    	
	    	//定义方法类型MethodType
	    	MethodType mt = MethodType.methodType(void.class);
	    	//新建方法句柄
	    	try {
				MethodHandle mh = MethodHandles.lookup().findVirtual(GrandFather.class, "thinking", mt);
				//调用
				mh.invokeExact(((new TestSon()).new GrandFather()));
			} catch (NoSuchMethodException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    	
	    }
	}

	public static void main(String[] args) {
		TestSon s = new TestSon();
		(s.new Son()).thinking();

	}

}

